package com.crois.barrier.admin.controller;


import com.crois.barrier.admin.dto.IpBlackWhiteListDto;
import com.crois.barrier.admin.dto.PluginDataDto;
import com.crois.barrier.admin.entity.IpBlackWhiteList;
import com.crois.barrier.admin.entity.PluginData;
import com.crois.barrier.admin.mapstruct.IpBlackWhiteDtoEntityMapper;
import com.crois.barrier.admin.mapstruct.PluginDataDtoEntityMapper;
import com.crois.barrier.admin.resp.BarrierResponseWrapper;
import com.crois.barrier.admin.service.IpBlackWhiteSvc;
import com.crois.barrier.common.response.BarrierResponseEntity;
import com.crois.barrier.common.utils.UtilsAndCommons;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 15:41 2021/10/27
 */
@RequestMapping(UtilsAndCommons.DEFAULT_BARRIER_ADMIN_IP_BLACK_WHITE_CONTEXT)
@RestController
public class IpBlackWhiteCtl {

    @Autowired
    @Qualifier("ipBlackWhiteSvc")
    private IpBlackWhiteSvc ipBlackWhiteSvc;

    /***
     * barrier ip black white list
     */
    @GetMapping(UtilsAndCommons.BARRIER_LIST_CONTEXT)
    public BarrierResponseEntity<List<?>> ipBlackWhiteList(){
        List<IpBlackWhiteList> ipBlackWhiteList = ipBlackWhiteSvc.findIpBlackWhiteList();
        return BarrierResponseWrapper.okList(IpBlackWhiteDtoEntityMapper.INSTANCE.toIpBlackWhiteListDtoList(ipBlackWhiteList));
    }


    /***
     * barrier ip black white list save
     */
    @PostMapping(UtilsAndCommons.BARRIER_ADD_CONTEXT)
    public BarrierResponseEntity<Void> ipBlackWhiteAdd(@RequestBody IpBlackWhiteListDto ipBlackWhiteListDto){
        IpBlackWhiteList ipBlackWhiteList = IpBlackWhiteDtoEntityMapper.INSTANCE.toIpBlackWhiteListEntity(ipBlackWhiteListDto);
        ipBlackWhiteSvc.saveIpBlackWhite(ipBlackWhiteList);
        return BarrierResponseWrapper.ok();
    }

    /***
     * barrier ip black white list update
     */
    @PutMapping(UtilsAndCommons.BARRIER_UPDATE_CONTEXT+"/{ipBlackWhiteId}")
    public BarrierResponseEntity<Void> ipBlackWhiteUpdate(@PathVariable("ipBlackWhiteId") Integer ipBlackWhiteId,@RequestBody IpBlackWhiteListDto ipBlackWhiteListDto){
        ipBlackWhiteListDto.setId(ipBlackWhiteId);
        ipBlackWhiteSvc.updateIpBlackWhite(IpBlackWhiteDtoEntityMapper.INSTANCE.toIpBlackWhiteListEntity(ipBlackWhiteListDto));
        return BarrierResponseWrapper.ok();
    }

    /***
     * barrier ip black white list delete
     */
    @DeleteMapping(UtilsAndCommons.BARRIER_DELETE_CONTEXT+"/{ipBlackWhiteId}")
    public BarrierResponseEntity<Void> ipBlackWhiteDelete(@PathVariable("ipBlackWhiteId") Integer ipBlackWhiteId){
        ipBlackWhiteSvc.removeIpBlackWhite(ipBlackWhiteId);
        return BarrierResponseWrapper.ok();
    }


}
